namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class ContactAssembler : AssemblerBaseUpdatable<KYO.DAL.Contact, ContactCriteria, ContactRowMap, ContactDomainObjectFactory> {
        public ContactAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.Contact ReadSingleByKey(int ContactId) {
            ContactCriteria criteria = new ContactCriteria();
            criteria.Set_ContactId(ContactCriteria.AVAILABLE_OPERATORS.EQUALS, ContactId);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.Contact PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }

            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                ContactUpdateFactory updateFactory = new ContactUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.CommandText = (comm.CommandText + "; SELECT SCOPE_IDENTITY();");
                    decimal NewIdentityKey = ((decimal)(comm.ExecuteScalar()));
                    PersistentObject.ContactId = ((int)(NewIdentityKey));
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;

        }

        internal override void Delete(KYO.DAL.Contact PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();

            ContactSelectionFactory tempContactSelectionFactory = new ContactSelectionFactory();
            string sql = "DELETE FROM [dbo].[Contact]";
            comm.CommandText = sql;
            tempContactSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Helper Utilities
        protected override ContactRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            ContactRowMap result = new ContactRowMap();
            result.ContactId = reader.GetInt32(reader.GetOrdinal("dboContactDOTContactId"));
            if ((reader.IsDBNull(reader.GetOrdinal("dboContactDOTFullName")) == false)) {
                result.FullName = reader.GetString(reader.GetOrdinal("dboContactDOTFullName"));
            }
            result.Company = reader.GetString(reader.GetOrdinal("dboContactDOTCompany"));
            result.Address = reader.GetString(reader.GetOrdinal("dboContactDOTAddress"));
            result.Telephone = reader.GetString(reader.GetOrdinal("dboContactDOTTelephone"));
            if ((reader.IsDBNull(reader.GetOrdinal("dboContactDOTFax")) == false)) {
                result.Fax = reader.GetString(reader.GetOrdinal("dboContactDOTFax"));
            }
            result.Email = reader.GetString(reader.GetOrdinal("dboContactDOTEmail"));
            if ((reader.IsDBNull(reader.GetOrdinal("dboContactDOTContent")) == false)) {
                result.Content = reader.GetString(reader.GetOrdinal("dboContactDOTContent"));
            }
            result.IsRead = reader.GetBoolean(reader.GetOrdinal("dboContactDOTIsRead"));
            result.Title = reader.GetString(reader.GetOrdinal("dboContactDOTTitle"));
            if ((reader.IsDBNull(reader.GetOrdinal("dboContactDOTDateCreate")) == false)) {
                result.DateCreate = reader.GetDateTime(reader.GetOrdinal("dboContactDOTDateCreate"));
            }
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + ContactAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[Contact]";
            }
        }
        public const string RawFields = @"[dbo].[Contact].[ContactId] AS dboContactDOTContactId, [dbo].[Contact].[Address] AS dboContactDOTAddress, [dbo].[Contact].[Company] AS dboContactDOTCompany, [dbo].[Contact].[Content] AS dboContactDOTContent, [dbo].[Contact].[DateCreate] AS dboContactDOTDateCreate, [dbo].[Contact].[Email] AS dboContactDOTEmail, [dbo].[Contact].[Fax] AS dboContactDOTFax, [dbo].[Contact].[FullName] AS dboContactDOTFullName, [dbo].[Contact].[IsRead] AS dboContactDOTIsRead, [dbo].[Contact].[Telephone] AS dboContactDOTTelephone, [dbo].[Contact].[Title] AS dboContactDOTTitle";
        public const string SELECT = @"SELECT [dbo].[Contact].[ContactId] AS dboContactDOTContactId, [dbo].[Contact].[Address] AS dboContactDOTAddress, [dbo].[Contact].[Company] AS dboContactDOTCompany, [dbo].[Contact].[Content] AS dboContactDOTContent, [dbo].[Contact].[DateCreate] AS dboContactDOTDateCreate, [dbo].[Contact].[Email] AS dboContactDOTEmail, [dbo].[Contact].[Fax] AS dboContactDOTFax, [dbo].[Contact].[FullName] AS dboContactDOTFullName, [dbo].[Contact].[IsRead] AS dboContactDOTIsRead, [dbo].[Contact].[Telephone] AS dboContactDOTTelephone, [dbo].[Contact].[Title] AS dboContactDOTTitle";
        #endregion
    }
}
